package com.googlecode.economini.converter;

import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

public class IssueConverter extends Converter<IssueToken> {

  private IssueConverter(TokenizerNode<IssueToken> root) {
    super(root);
  }

  public static IssueConverter newInstance() {
    TokenizerNode<IssueToken> sectionNode = TokenizerNode.build(
        "h4",
        AttributeMatcher.none(),
        TokenFactories.valueTokenFacotry(IssueToken.SECTION_TITLE));

    AttributeMatcher articleAttributes = new AttributeMatcher.Builder()
        .matchesAll()
        .withAttribute("class", "node-link")
        .build();

    TokenizerNode<IssueToken> articleNode = TokenizerNode.build(
        "a",
        articleAttributes,
        TokenFactories.compoundTokenFacotry(
            IssueToken.ARTICLE_TITLE,
            "href",
            IssueToken.ARTICLE_URL));

    TokenizerNode<IssueToken> articleDiv = TokenizerNode.buildNodeForPath(
        ImmutableList.of("div"), ImmutableList.of(articleNode));

    TokenizerNode<IssueToken> root = TokenizerNode.buildNodeForPath(
        Lists.newArrayList(Splitter.on('/').split(
            "root/html/body/div/div/div/div/div/div/div/div/div")), 
        ImmutableList.of(sectionNode, articleDiv));

    return new IssueConverter(root);
  }
}
